<?php
/**
 * Erstellung und verwaltung von priorisierten Listeneintraegen
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2010 Loresoft Software (http://www.loresoft.de)
 *
 * */


Class Yapary_List_PriorityList
{
    /** @var $oSql Yapary_Database_Sql */
    private $oSql           = NULL;
    /** @var $dbTableName string */
    private $dbTableName    = 'yapary_list_priority';


    /**
     * @param Object $_oSql	: Datenbankklasse fuer Datenbankzugriff
     * */
    function __construct (Yapary_Db_Sql_Connector $_oSql)
    {
        $this->oSql  = $_oSql;
    }


    /**
     * Fuegt einen neuen Eintrag hinzu
     * @return Yapary_List_Priority|NULL
     * */
    public function add ($_name, $_prio = 0, $_content = NULL)
    {
        $oDbResult = $this->oSql->request('INSERT INTO '.$this->dbTableName.' (Name, Content, Prio) VALUES (:name, :content, :prio)',
                                 array('name'    => $_name,
                                       'content' => base64_encode(serialize(array())),
                                       'prio'    => $_prio));

        $lastId = mysql_insert_id();

        $oPriority = $this->get($lastId);
        $arr = $this->getList($oPriority->getName());
        $order = count($arr);

        $oStructure = new Yapary_List_Priority($this->oSql, $lastId);
        $oStructure->setOrder($order);
        if ($_content != NULL) {
           $oStructure->setContent($_content);
        }
        $oStructure->save();
        return $oStructure;

        return true;
    }


    /**
     * Loescht einen existierenden Eintrag
     * @return bool
     * */
    public function delete ($_id)
    {
        $i = 0;
        $oPriority = $this->get($_id);
        $arr = $this->getList($oPriority->getName());

        $count = count($arr);
        $oDbResult = $this->oSql->request('DELETE
                					    FROM '.$this->dbTableName.'
                					    WHERE Id=:id',
                                      array('id' => $_id));

        if ($count >= 1) {
            while (list(,$class) = each($arr)) {
                if ($class->getId() != $_id) {
                    $class->setOrder($i++);
                    $class->save();
                }
            }
        }

        return true;
    }


    /**
     * Benennt einen vorhandenen Eintrag um
     * @param string $_name		: Name des Elementenpaars
     * @return bool
     * */
    public function rename ($_name)
    {
        $oDbResult = $this->oSql->request('UPDATE '.$this->dbTableName.'
                					 SET Name=:newName
                					 WHERE Name=:oldName',
                                    array('newName' => $_name,
                                          'oldName' => $oldName));
        return true;
    }


    /**
     * Liefert einen Datensatz anhand der Id
     * @return Yapary_List_Priority
     * */
    public function get ($_id)
    {
        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Id=:id',
                            array('id' => $_id));

        if ($oDbResult->getCount() == 1) {
            $row = $oDbResult->getNext();
            return new Yapary_List_Priority($this->oSql, $row);
        }

        return NULL;
    }


    /**
     * Liefert alle Datensaetz der 2.Kategorie
     * @return Array[Yapary_List_Priority]
     * */
    public function getList ($_name, $_orderBy = 'AscOrder')
    {
        $arrResult = array();
        $_orderBy = (preg_match('/^([a-zA-Z-_ ]+)([a-zA-Z-_0-9 ]*)$/', $_orderBy)) ? ' ORDER BY '.$_orderBy : '';

        $oDbResult = $this->oSql->request('SELECT *
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Name=:name
        					  '.$_orderBy.'',
                            array('name' => $_name));

        if ($oDbResult->getCount() >= 1) {
            while ($row = $oDbResult->getNext()) {
                $arrResult[] = new Yapary_List_Priority($this->oSql, $row);
            }
        }

        return $arrResult;
    }


    /**
     * Erhoeht die Orderritaet um 1
     * @return bool
     * */
    public function setOrderUp ($_id)
    {
        $oPriority = $this->get($_id);
        $arr = $this->getList($oPriority->getName());
        $count = count($arr);

        if ($count > 1) {
            $objA = NULL;
            $objB = NULL;
            while (list(, $class) = each($arr)) {
                /*@var $class Yapary_Page_Structure*/
                $objA = $objB;
                $objB = $class;

                if ($objB->getId() == $_id && is_object($objA)) {
                    $prioA = $objA->getOrder();
                    $prioB = $objB->getOrder();

                    $objA->setOrder($prioB);
                    $objB->setOrder($prioA);
                    if ($objA->save() && $objB->save()) {
                        return true;
                    }
                }
            }
        }

        return false;
    }


    /**
     * Erniedrigt die Orderritaet um 1
     * @return bool
     * */
    public function setOrderDown ($_id)
    {
        $oPriority = $this->get($_id);
        $arr = $this->getList($oPriority->getName());
        $count = count($arr);

        if ($count > 1) {
            $objA = NULL;
            $objB = NULL;
            for ($i=0; $i<$count; $i++) {
                $objA = $arr[$i];

                if ($objA->getId() == $_id) {
                    if ($i+1 != $count) { // Wenn Id nicht das letzte Element, dann tausche mit naechstem Element
                        $objB = $arr[$i+1];

                        $prioA = $objA->getOrder();
                        $prioB = $objB->getOrder();

                        $objA->setOrder($prioB);
                        $objB->setOrder($prioA);
                        if ($objA->save() && $objB->save()) {
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }


    /**
     * Ermittelt die Anzahl an Eintaegen
     * @return int
     * */
    public function getCount ($_name)
    {
        $oDbResult = $this->oSql->request('SELECT Id
        					  FROM '.$this->dbTableName.' t1
        					  WHERE Name=:name',
                            array('name' => $_name));

        return $oDbResult->getCount();
    }


}


?>